// 是否科目表数结构余额改色
frappe.db.get_single_value("shezhi", "kemubiaoshujiegou_gaise").then(是否 => {
    console.log(`是否 👉`, 是否)

    if (是否) {
        // 备份原始方法
        var originalOnGetNode = frappe.treeview_settings["Account"].on_get_node
        console.log(`originalOnGetNode 👉`,originalOnGetNode)
        originalOnGetNode = function (nodes, deep = false) {
            console.log(`on_get_node ing👇`)
            if (frappe.boot.user.can_read.indexOf("GL Entry") == -1) return

            let accounts = []
            if (deep) {
                accounts = nodes.reduce((acc, node) => [...acc, ...node.data], [])
            } else {
                accounts = nodes
            }

            frappe.db.get_single_value("Accounts Settings", "show_balance_in_coa").then(value => {
                if (value) {
                    const get_balances = frappe.call({
                        method: "erpnext.accounts.utils.get_account_balances",
                        args: {
                            accounts: accounts,
                            company: cur_tree.args.company,
                        },
                    })

                    get_balances.then(r => {
                        console.log(`get_balances r 👉`,r)
                        if (!r.message || r.message.length == 0) return

                        for (let account of r.message) {
                            const node = cur_tree.nodes && cur_tree.nodes[account.value]
                            if (!node || node.is_root) continue

                            // 修改为中文借贷标识和颜色
                            const balance = account.balance_in_account_currency || account.balance
                            // const dr_or_cr = balance > 0 ? "Dr" : "Cr"

                            const dr_or_cr = balance > 0 ? "借" : "贷"
                            const format = (value, currency) => format_currency(Math.abs(value), currency)
                            const color = dr_or_cr === "借" ? "blue" : "red"

                            if (account.balance !== undefined) {
                                node.parent && node.parent.find(".balance-area").remove()
                                $(
                                    `<span class="balance-area pull-right" style="color: ${color} !important;">` +
                                        (account.balance_in_account_currency
                                            ? format(
                                                  account.balance_in_account_currency,
                                                  account.account_currency
                                              ) + " / "
                                            : "") +
                                        format(account.balance, account.company_currency) +
                                        " " +
                                        dr_or_cr +
                                        "</span>"
                                ).insertBefore(node.$ul)
                            }
                        }
                    })
                }
            })
        }
        console.log(`originalOnGetNode 👉`,originalOnGetNode)
    }
})
